package com.bsoft.gol.hbs.dao;

import ctd.controller.exception.ControllerException;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import hc.message.entity.MsgTplWx;
import org.atag.dao.util.BaseDaoImpl;
import org.hibernate.StatelessSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @describe 消息微信公众号实体类
 */
@SsdevDao(serviceId = "msgTplWxService")
public abstract class MsgTplWxDao extends BaseDaoImpl<MsgTplWx> {
    private static final Logger logger = LoggerFactory.getLogger(MsgTplWxDao.class);

    public MsgTplWxDao() {
        this.setKeyField("wxTplId");
    }

    @RpcService
    @DAOMethod(sql = "from MsgTplWx where wxTplId=:wxTplId")
    public abstract MsgTplWx getMsgTplWxById(@DAOParam("wxTplId") String wxTplId);

    @DAOMethod(sql = "from MsgTplWx where appId=:appId and tplCode=:tplCode")
    @RpcService
    public abstract MsgTplWx getByAppIdAndTplCode(@DAOParam("appId") String appId, @DAOParam("tplCode") String tplCode);

    @DAOMethod(sql = "from MsgTplWx")
    @RpcService
    public abstract List<MsgTplWx> findMsgTplWxs();

    /**
     * @return
     * @throws
     * @describe 查询消息code查询模板总数
     * @author zhengcb10708
     * @date 2020/9/22 13:04
     */
    @RpcService
    public Long getMsgTplWxeCountByWxTplId(final String wxTplId) {
        Map<String, Object> params = new HashMap<>();

        StringBuffer hsql = new StringBuffer();
        hsql.append("select count(1)  from msg_tpl_wx  a  ");
        hsql.append("  where 1=1 ");
        if (wxTplId != null && !wxTplId.isEmpty()) {
            hsql.append(" and  a.wx_tpl_id = :wxTplId");
            params.put("wxTplId", wxTplId);
        }
        return super.findForLongBySql(hsql.toString(), params);
    }

    /**
     * @throws
     * @describe 新增微信消息模板配置
     * @author zhengcb10708
     * @date 2020/9/21 15:59
     */
    @RpcService
    public void addMsgTemplateWxConfigure(final MsgTplWx msgTplWx) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                ss.insert(msgTplWx);
            }
        });
    }

    /**
     * @throws
     * @describe 修改微信消息模板配置
     * @author zhengcb10708
     * @date 2020/9/21 14:14
     */
    @RpcService
    public void updateMsgTemplateWxConfigure(final MsgTplWx msgTplWx) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                //更新微信模板配置信息表
                ss.update(msgTplWx);
            }
        });
    }

    @RpcService
    public void saveMsgTplWx(MsgTplWx msgTplWx) {
        this.save(msgTplWx);
    }

    @RpcService
    public void updateMsgTplWx(MsgTplWx msgTplWx) {
        this.update(msgTplWx);
    }

    @RpcService
    public void deleteMsgTplWx(Integer msgTplIId) {
        this.remove(msgTplIId);
    }

}
